package cn.coisini.controller.v1;

import cn.coisini.api.admin.MenuControllerApi;
import cn.coisini.model.common.annotation.RedisAccessLimit;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.common.log.annotation.Log;
import cn.coisini.model.common.log.enums.BusinessType;
import cn.coisini.model.pojo.Menu;
import cn.coisini.model.vo.AssginMenuVo;
import cn.coisini.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiangshaw
 * Description: 菜单管理
 */
@RequiredArgsConstructor
@RestController
public class MenuController implements MenuControllerApi {

    private final MenuService menuService;

    // 菜单列表
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<List<Menu>> findNodes() {
        return menuService.findNodes();
    }

    // 根据id查询菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<Menu> info(Long id) {
        return Result.ok(menuService.getById(id));
    }

    // 添加菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    public Result<ResultEnum> saveMenu(Menu menu) {
        return menuService.saveMenu(menu);
    }

    // 修改菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    public Result<ResultEnum> updateMenu(Menu menu) {
        return menuService.updateMenuById(menu);
    }

    // 删除菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    public Result<ResultEnum> remove(Long id) {
        return menuService.removeMenuById(id);
    }

    // 根据角色id查询菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<List<Menu>> toAssign(String id) {
        return menuService.findMenuByRoleId(id);
    }

    // 给角色分配菜单
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "菜单管理", businessType = BusinessType.ASSGIN)
    public Result<ResultEnum> doAssign(AssginMenuVo menuVo) {
        return menuService.doAssign(menuVo);
    }

    // 菜单状态修改
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "菜单管理", businessType = BusinessType.STATUS)
    public Result<ResultEnum> status(String id, Boolean status) {
        return menuService.updateStatus(id, status);
    }
}
